import * as fgui from "fairygui-cc";
import { Log } from "../../../../../Core/Module/Log/Log";
import { ObjectSystemAttribute, FriendOf, ExpandOf } from "../../../../../Decorator/Decorator";
import { FUIEntity } from "../../../../ModelView/Client/Plugins/FairyGUI/FUIEntity";
import { AwakeSystem } from "../../../../../Core/Module/EventSystem/IAwakeSystem";
import { DestroySystem } from "../../../../../Core/Module/EventSystem/IDestroySystem";
import { PanelCoreData } from "../../../../ModelView/Client/Plugins/FairyGUI/PanelDataDefine";
import { PanelId } from "../../../../ModelView/Client/Demo/FUIAutoGen/PanelId";

/**
 * 使用 Expand 扩展方法
 * 
 * 必须作出以下申明，才能调用对象的扩展方法
 */
declare module "../../../../ModelView/Client/Plugins/FairyGUI/FUIEntity" {

    interface FUIEntity {
        SetRoot(rootGComponent: fgui.GComponent): void;
    }

}

@ExpandOf(FUIEntity)
export class FUIEntitySystem {

    private static SetRoot(self: FUIEntity, rootGComponent: fgui.GComponent): void {
        if (self.GComponent == null) {
            Log.Error(`FUIEntity ${self.PanelId} GComponent is null!!!`);
            return;
        }
        if (rootGComponent == null) {
            Log.Error(`FUIEntity ${self.PanelId} rootGComponent is null!!!`);
            return;
        }
        rootGComponent.addChild(self.GComponent);
    }

}

@FriendOf(FUIEntity)
@ObjectSystemAttribute()
export class FUIEntityAwakeSystem extends AwakeSystem<FUIEntity> {

    Awake(self: FUIEntity): void {
        self.PanelCoreData = self.AddChild<PanelCoreData>(PanelCoreData);
    }

}

@FriendOf(FUIEntity)
@ObjectSystemAttribute()
export class FUIEntityDestroySystem extends DestroySystem<FUIEntity> {

    Destroy(self: FUIEntity): void {
        self.PanelCoreData?.Dispose();
        self.PanelId = PanelId.Invalid;
        if (self.GComponent != null) {
            self.GComponent.dispose();
            self.GComponent = null;
        }
    }

}